package iscript.uiFramework.components.form 

{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.events.MouseEvent;
	import iscript.uiFramework.components.button.IButton;
	import iscript.uiFramework.components.InteractiveUIComponent;
	import iscript.uiFramework.components.text.Label;
	import iscript.uiFramework.data.ListBoxModel;
	import iscript.uiFramework.event.ListBoxEvent;
	import iscript.uiFramework.event.ListBoxEvent;
	
	
	
	[Event(name="CellRender_Select", type = "iscript.uiFramework.event.ListBoxEvent")]
	public class ListCellRender extends InteractiveUIComponent
	{
		private var _bgBm:Bitmap;
		private var _label:Label;
		private var _icon:BitmapData;
		private var _selected:Boolean;
		public var position:int;
		private var _model:ListBoxModel;
		public var index:int;
		
		public function ListCellRender(model:ListBoxModel,preferWidth:Number = 100,preferHeight:Number=20)
		{
			_model = model;
			HoverHandler = hoverHandler;
			OutHandler = outHandlerLogic;
			DownHandler = downHandlerLogic;
			super(preferWidth, preferHeight);
		}
		
		override protected function initialize():void
		{
			_bgBm = new Bitmap(new BitmapData(_preferWidth,_preferHeight,false,0xFFFFFF))
			addChild(_bgBm);
			_label = new Label();
			_label.x = 2;
			_label.y = 2;
			_label.render(this);
			super.initialize();
		}
		
		override protected function eventListener():void 
		{
			//this.addEventListener(MouseEvent.DOUBLE_CLICK,_ListCellOpenLogic)
			
			super.eventListener();
		}
		
	/*	private function _ListCellOpenLogic(e:MouseEvent):void 
		{
			
		}*/
		
		
		override public function set width(value:Number):void
		{
			super.width = value;
			_bgBm.width = value;
		}
		
		override public function set height(value:Number):void
		{
			super.height = value;
			_bgBm.height = value;
		}
		
		public function get text():String
		{
			return _label.text;
		}
		
		public function set text(value:String):void
		{
			_label.text = value;
		}
		
		public function get icon():BitmapData
		{
			return _label.icon;
		}
		
		public function set icon(value:BitmapData):void
		{
			_label.icon = value;
		}
		
		public function get selected():Boolean 
		{
			return _selected;
		}
		
		public function set selected(value:Boolean):void 
		{
			_selected = value;
		}
		
		public function get value():ListBoxModel 
		{
			return _model;
		}
		
		public function hoverHandler(e:*):void {
			if(!_selected)
			_bgBm.bitmapData = new BitmapData(_bgBm.bitmapData.width, _bgBm.bitmapData.height, false, 0xCADBE6);
		}
		
		private function outHandlerLogic(e:*):void 
		{
			if(!_selected)
			_bgBm.bitmapData = new BitmapData(_bgBm.bitmapData.width,_bgBm.bitmapData.height,false,0xFFFFFF)	
		}
		
		override public function normal():void 
		{
			super.normal();
			_bgBm.bitmapData = new BitmapData(_bgBm.bitmapData.width, _bgBm.bitmapData.height, false, 0xFFFFFF);
			_label.color = 0x000000;
			_selected = false;
		}
		
		private function downHandlerLogic(e:*):void 
		{
			_label.color = 0xFFFFFF;
			_selected = true;
			_bgBm.bitmapData = new BitmapData(_bgBm.bitmapData.width, _bgBm.bitmapData.height, false, 0x77A3BF);
			var event:ListBoxEvent = new ListBoxEvent(ListBoxEvent.CellRender_Select)
			    event.selectIndex = position;
				event.selectData = _model;
			this.dispatchEvent(event);
		}
		
		override public function render(container:DisplayObjectContainer = null):void 
		{
			_label.text = _model.text;
			_label.icon = _model.bitmap;
			//border = true;
			//getBorder().color = 0xEFEFEF
			super.render(container);
		}

		
	}

}